﻿using HomagChina.Basic.CCT.Db.CCHS_Db;
using HomagChina.CCHCS.Views;
using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Controls;
using Proxy;
using System;
using System.Windows.Controls;
using static HomagChina.CCHCS.Utility;

namespace HomagChina.CCHCS.ViewModels
{
    public class StockInfoEditingViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private SearchInfoHandle Handle_Search;
        private InsertInfoHandle Handle_Insert;
        private ModifyInfoHandle Handle_modify;
        private CommonHandle commonHandle;

        #endregion Handle

        #region 属性

        public RelayCommand SaveCommand { get; }
        public RelayCommand CancelCommand { get; }
        private int? _ActualQuantity;
        public T_CCHS_Stock StockModel { get; set; }
        public DropDownList DropDownList_Material { get; set; }

        public int? ActualQuantity
        {
            get => _ActualQuantity;
            set { _ActualQuantity = value; OnPropertyChanged(); }
        }

        public object SelectedValue;

        #endregion 属性

        public StockInfoEditingViewModel(T_CCHS_Stock model)
        {
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_modify = new ModifyInfoHandle();
            commonHandle = new CommonHandle();

            LoadDropDownList();
            SaveCommand = new RelayCommand(Save);
            CancelCommand = new RelayCommand(Cancel);
            StockModel = model;
            DropDownList_Material.SelectedItem = model.Material;
            ActualQuantity = model.ActualQuantity;
        }

        public void LoadDropDownList()
        {
            DropDownList_Material = new DropDownList();
            var materialList = Handle_Search.GetMaterialList();
            materialList.Add("Add new material");
            DropDownList_Material.ItemsSource = materialList;
            SelectedValue = DropDownList_Material.Items.CurrentItem;
            DropDownList_Material.SelectedIndex = 0;
            DropDownList_Material.SelectionChanged += DropDownList_SelectionChanged;
        }

        private void DropDownList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedValue = ((DropDownList)sender).SelectedItem.ToString();
            if (SelectedValue.Equals("Add new material"))
            {
                BoardEditingViewModel boardEditingViewModel = new BoardEditingViewModel(null, false);
                UIAdjustments.CurrentThreadDialogManager.ShowDialog<BoardEditing>(boardEditingViewModel, null);
            }
        }

        public void Save()
        {
            if (SelectedValue.Equals("Add new material"))
            {
                return;
            }
            try
            {
                Handle_modify.ModifyStockInfo(StockModel, SelectedValue.ToString(), (int)ActualQuantity);
                commonHandle.CloseDiaLog("EditStockInfo");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            ;
        }

        public void Cancel()
        {
            CloseDiaLog("EditStockInfo");
        }
    }
}